extends Node2D

class_name Planet
onready var MAIN = find_parent("MAIN")
var SHIP 
var TOP_PANEL

export var color : Color
export var mass=100
var radius
var planet_visual
var rings = []

var jam = null # can be shooter or strategy
var jam_till_start = 0
var jam_till_finish = 0

var JAM_MIN_DURATION = 2
var JAM_MAX_DURATION = 10

var JAM_MIN_TIME_TO_START = 10
var JAM_MAX_TIME_TO_START = 20


#var jamstart_timer
#var jamstop_timer
var jam_timer
var jam_missed_timer

var jam_missed_timeout = 1.0

var jam_state

var labelnode
var label

enum {
	STRATEGY,
	SHOOTER
}

enum {
	STARTING
	IN_PROGRESS
}

var jam_names = {
	STRATEGY: "STRATEGY",
	SHOOTER: "SHOOTER"
}

var jam_dists = {
	STRATEGY: 80.0,
	SHOOTER: 60.0,
}

var jam_rewards = {
	STRATEGY: 0.5,
	SHOOTER: 1.0,
}

var ring_percents = [
	0.3,
	0.5,
	1.0
]

var ring_colors = [
	ColorN("cyan"),
	ColorN("yellow"),
	ColorN("magenta")
]

# Called when the node enters the scene tree for the first time.
func _ready():
	radius = sqrt(mass)*3
	randomize()
	planet_visual = Node2D.new()
	var circle = Circle.new(radius)
	circle.color = color
	planet_visual.add_child(circle)
	add_child(planet_visual)
	
	labelnode = Node2D.new()
	label = FontedLabel.new()
	add_child(labelnode)
	labelnode.visible = false
	labelnode.z_index = 2
	labelnode.add_child(label)
	label.rect_position = - Vector2(0, radius*2.5)
	label.align = HALIGN_CENTER
	label.grow_horizontal = Control.GROW_DIRECTION_BOTH
	label.text = ""
	labelnode.visible = false
	
	jam_timer = Timer.new()
	jam_missed_timer = Timer.new()
	call_deferred("add_child",jam_timer)
	call_deferred("add_child",jam_missed_timer)
	
	jam_missed_timer.wait_time = jam_missed_timeout
	jam_missed_timer.one_shot = true
	
	jam_timer.wait_time = 1.0
	jam_timer.connect("timeout",self,"on_jam_tick")
	jam_missed_timer.connect("timeout",self,"on_missed_jam_tick")
	
	MAIN.planets.append(self)

func on_jam_tick():
	match(jam_state):
		STARTING:
			update_label_starting()
			if jam_till_start <= 0:
				start_jam()
			jam_till_start -= 1
		IN_PROGRESS:
			label.text = "FINISHES" + " IN " + str(jam_till_finish)
			if jam_till_finish <= 0:
				finish_jam()
			jam_till_finish -= 1

func update_label_starting():
	label.text = jam_names[jam] + " IN " + str(jam_till_start) + " FOR " + str(jam_till_finish)

func on_missed_jam_tick():
	print ("missed_jam fires")
	if jam == null:
		labelnode.visible = false
		label.text = ""
	

func start_jam():
	print(jam_names[jam] + " tries to start")
	if jam == null:
		print(jam_names[jam] + " not a jam")
		return
	jam_state = IN_PROGRESS
	if SHIP == null:
		SHIP = MAIN.get_node("SHIP") # the order of entry into the tree is undefined
	if (SHIP.global_position - global_position).length() > rings[-1].radius:
		jam_missed()
	else:
		if TOP_PANEL == null:
			TOP_PANEL = MAIN.find_node("top_panel") # the order of entry into the tree is undefined
		TOP_PANEL.activate(self)

func jam_missed():
	label.text = 'MISSED IT!'
	print ("jam missed")
	jam_missed_timer.start()
	finish_jam()
	labelnode.visible = true

func add_random_jam():
	jam = randi() % 2
	print("jam pops up")
	make_rings()
	labelnode.visible = true
	
	jam_till_start = floor(rand_range(JAM_MIN_TIME_TO_START,JAM_MAX_TIME_TO_START))
	jam_till_finish = floor(rand_range(JAM_MIN_DURATION,JAM_MAX_DURATION))
	
	jam_state = STARTING
	jam_timer.start()
	update_label_starting()

func finish_jam():
	jam_timer.stop()
	for r in rings:
		r.queue_free()
	rings.clear()
	jam = null
	labelnode.visible = false
	if TOP_PANEL == null:
			TOP_PANEL = MAIN.find_node("top_panel") # the order of entry into the tree is undefined
	TOP_PANEL.deactivate()

func make_rings():
	for i in range(len(ring_colors)):
		var ring = CircleOutline.new(ring_percents[i]*jam_dists[jam] + radius)
		ring.default_color = ring_colors [i]
		ring.width = 2
		rings.append(ring)
		call_deferred("add_child",ring)
